package engine.logic.login
{
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.InteractiveObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	/**
	 * 滚动动画效果 ，只管理一个
	 * 重复的开始滚动新的目标会自动清除前一个
	 * @author guyi
	 * 
	 */	
	public class ScrollAnimation
	{
		
		private var _nxTimer:NxTimer;
		
		private var _target:InteractiveObject;
		private var _targetTrueSize:Rectangle;
		
		private var _originBg:Object;
		private var _bg:Rectangle;
		
		private var _speed	:	Number	=	0.6;
		private var _delay	:	Number	=	20;
		
		private const UpOverBound		:	int	=	1;
		private const DownOverBound	:	int	=	2;
		private const BingGo			:	int	=	-1;
		
		private var bitSprit:Sprite	=	new Sprite();
		private var bitMap:BitmapData	=	new BitmapData(1,1);
		public function ScrollAnimation()
		{
		}
		
		/**
		 * 开始滚动目标 ,重复的开始新的目标会自动清除前一个
		 * @param target
		 * @param viewRect 一个矩形。位置无所谓
		 * @param speed
		 * @param isCircle
		 * @param startPer 从哪个位置开始播，默认1/2
		 */		
		public function start(target:InteractiveObject,viewRect:Object,speed:Number=0.2,isCircle:Boolean	=false,startPer:Number=0.5):void
		{
			if(	null	==	_nxTimer)
					_nxTimer=new NxTimer();
			
			this.stop();
			
			_target	=	target;
			_speed	=	speed;
			
			_originBg	=	viewRect;
			_bg			=	new Rectangle(0,-viewRect.height*startPer,viewRect.width,viewRect.height);
			
			_targetTrueSize=	this.getTrueSize(target);
			if(	null	==	_targetTrueSize)
			{
				throw new Error("无法获取到目标的真实大小，滚动失败");
				return;
			}
			
			_target.scrollRect=	new Rectangle(0,0,_bg.width,_bg.height);
			_target.addEventListener(MouseEvent.MOUSE_OVER,overHandler);
			_target.addEventListener(MouseEvent.MOUSE_OUT,outHandler);
			_target.addEventListener(MouseEvent.MOUSE_WHEEL,wheelHandler);
			
			_nxTimer.timingCall(scrollAnimation,[_speed],_delay,0,true);
		}
		
		
//		/**
//		 * 重置动画 
//		 * @param isTerminate,是否停止
//		 * 
//		 */		
//		public function reset(isTerminate:Boolean=false,newViewSize:Object=null):void
//		{
//			if(	null	!=	_target)
//			{
//				_bg.y=0;
//				_target.scrollRect	=	_bg;
//			}
//			
//			if(	null	!=	newViewSize)
//			{
//				_targetTrueSize
//			}
//			
//			if(isTerminate	&&	_nxTimer)
//				_nxTimer.stop();
//		}
		
		/**
		 * 暫停播放 
		 * 
		 */		
		public function stop():void
		{
			if(_nxTimer)
				_nxTimer.stop();
			
			if(	null	!=	_target)
			{
				_target.removeEventListener(MouseEvent.MOUSE_OVER,overHandler);
				_target.removeEventListener(MouseEvent.MOUSE_OUT,outHandler);
				_target.removeEventListener(MouseEvent.MOUSE_WHEEL,wheelHandler);
				_target	=	null;
			}
			
			_bg			=	null;
			_originBg	=	null;
		}
		
		private function overHandler(e:MouseEvent):void
		{
			_nxTimer.stop();
		}
		
		private function outHandler(e:MouseEvent):void
		{
			_nxTimer.timingCall(scrollAnimation,[_speed],_delay,0,true);
		}
		
		private function wheelHandler(e:MouseEvent):void
		{	
			scrollAnimation(-e.delta*1.5);
		}
		
		private function scrollAnimation(speed:Number):void
		{
			var legalityType:int	=	this.legalityCheck();
			
			if(	this.BingGo		==	legalityType)
			{
				_bg.y	+=	speed;
				_target.scrollRect	=	_bg;
			}
			else if(this.UpOverBound		==	legalityType)
			{
				if(speed>0)
				{
					_bg.y	+=	speed;
					_target.scrollRect	=	_bg;
				}
			}
			else if(this.DownOverBound		==	legalityType)
			{
				if(speed<0)
				{
					_bg.y	+=	speed;
					_target.scrollRect	=	_bg;
				}
			}
			
		}
		
		/**
		 * 检查目标但前合法性
		 * @return -1,合法。1,上边界不合法,2下边界不合法 .以数小者优先
		 */		
		private function legalityCheck():int
		{
//			_targetTrueSize	=	DrawAPI.getTrueRect(_target);
			
			if(_target.scrollRect.y	<	0)
			{
				return this.UpOverBound;
			}
			
			if( _target.scrollRect.y > _targetTrueSize.height-_bg.height)
			{
				return this.DownOverBound;
			}
			

			return this.BingGo;
		}
		
		private function clear():void
		{
			if(	null	!=	_target)
			{
				_target.removeEventListener(MouseEvent.MOUSE_OVER,overHandler);
				_target.removeEventListener(MouseEvent.MOUSE_OUT,outHandler);
				_target	=	null;
			}
			
			_bg			=	null;
			_originBg	=	null;
		}
		
		
		private function getTrueSize(dis:DisplayObject):Rectangle
		{
			var targetSize:Rectangle;
			var tempRect:Rectangle;
			if(dis != null)
			{
				if(dis.scrollRect != null)
				{
					tempRect	=	dis.getBounds(dis);
					dis.scrollRect	=	null;
					bitMap.draw(bitSprit);
					targetSize	=	dis.getBounds(dis);
					dis.scrollRect	=	tempRect
				}
				else
				{
					targetSize	=	dis.getBounds(dis);
				}

			}
			return targetSize;
//			var rect:Rectangle	= dis.scrollRect;
//			
//			var dx:Number	=0;
//			var dy:Number	=0;
//			var dwidth:Number	=0;
//			var dheight:Number	=0;
//			var trueSize:Rectangle;
//			
//			if(dis is TextField)
//			{
//				dwidth	=TextField(dis).textWidth;
//				dheight	=TextField(dis).textHeight;
//				
//				if(rect is Rectangle)
//				{
//					dx	=dis.x	-	rect.x;
//					dy	=dis.y	-	rect.y;
//				}
//				trueSize	=new Rectangle(dx,dy,dwidth,dheight);
//			}
//			else if(dis is DisplayObjectContainer)
//			{
//				trueSize	=	DrawAPI.getTrueRect(dis as DisplayObjectContainer);
//			}
//			
//			return	trueSize;
		}
	}
}